import time

from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

from Common.Utils import ChatDriver


class ChatList:
    url = ""
    driver = ""

    #  在登陆的状态下
    def ChatListByLogin(self):
        self.driver = ChatDriver.driver
        #  查找主页面的元素

        # 搜索昵称
        self.driver.find_element(By.CSS_SELECTOR, "body > div.client-container > div > div.left > div.user")

        # 查找搜索框和按钮
        self.driver.find_element(By.CSS_SELECTOR,
                                 "body > div.client-container > div > div.left > div.search > input[type=text]")
        self.driver.find_element(By.CSS_SELECTOR, "body > div.client-container > div > div.left > div.search > button")
        # 消息列表+消息列表按钮+联系人列表+联系人按钮
        self.driver.find_element(By.CSS_SELECTOR, "#session-list")
        self.driver.find_element(By.CSS_SELECTOR,
                                 "body > div.client-container > div > div.left > div.tab > div.tab-session")
        self.driver.find_element(By.CSS_SELECTOR, "#friend-list")
        self.driver.find_element(By.CSS_SELECTOR,
                                 "body > div.client-container > div > div.left > div.tab > div.tab-friend")

        # 消息列表
        self.driver.find_element(By.CSS_SELECTOR,
                                 "body > div.client-container > div > div.left > div.tab > div.tab-session").click()
        time.sleep(1)
        self.driver.find_element(By.CSS_SELECTOR, "#session-list > li:nth-child(2)").click()
        WebDriverWait(self.driver, 5).until(EC.presence_of_element_located(
            (By.CSS_SELECTOR, "body > div.client-container > div > div.right > div.message-show")))
        time.sleep(1)
        ChatDriver.getScreenShort()

        # 联系人列表
        self.driver.find_element(By.CSS_SELECTOR,
                                 "body > div.client-container > div > div.left > div.tab > div.tab-friend").click()

        WebDriverWait(self.driver, 5).until(EC.presence_of_element_located((By.CSS_SELECTOR, "#friend-list")))
        time.sleep(1)
        ChatDriver.getScreenShort()
        self.driver.find_element(By.XPATH, "/html/body/div[2]/div/div[1]/ul[2]/li/h4").click()
        WebDriverWait(self.driver, 5).until(EC.presence_of_element_located((By.CSS_SELECTOR, "#session-list")))
        time.sleep(1)
        ChatDriver.getScreenShort()

        # 聊天功能
        self.driver.find_element(By.CSS_SELECTOR,
                                 "body > div.client-container > div > div.left > div.tab > div.tab-session").click()
        self.driver.find_element(By.CSS_SELECTOR, "#session-list > li.selected").click()
        self.driver.find_element(By.CSS_SELECTOR, "body > div.client-container > div > div.right > div.title")
        self.driver.find_element(By.CSS_SELECTOR, "body > div.client-container > div > div.right > textarea").send_keys(
            "要不要出去走走？")
        self.driver.find_element(By.CSS_SELECTOR,
                                 "body > div.client-container > div > div.right > div.ctrl > button").click()
        time.sleep(1)
        ChatDriver.getScreenShort()

    # 在未登录状态下
    def ChatListNoLogin(self):
        # 在未登录的情况下使用登录成功的url
        self.url = "http://127.0.0.1:8080/client.html"
        self.driver = ChatDriver.driver
        self.driver.get(self.url)

        # 出现警告弹窗
        alert = WebDriverWait(self.driver, 5).until(EC.alert_is_present())
        # 获取弹窗文本
        text = alert.text
        # 断言弹窗文本与预期是否符合
        assert text == "当前用户未登录!"
        time.sleep(1)
        # 添加弹窗截图
        ChatDriver.get_alert_image()
        alert.dismiss()
